package base

import (
	"fmt"
	log "github.com/sirupsen/logrus"
	"os"
	"os/signal"
	"reflect"
	"syscall"
)

type HookStarter struct {
	BaseStarter
}

func (s *HookStarter) Start(ctx StarterContext) {

	//监听信号量
	sigs := make(chan os.Signal, 1)
	signal.Notify(sigs, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
	go func() {
		for {
			<-sigs
			go s.Stop(ctx)
		}
	}()

}
func (s *HookStarter) Stop(ctx StarterContext) {

	fmt.Println("-------------------------------")
	fmt.Println("stopping server")
	fmt.Println("-------------------------------")

	startes := GetStarters()
	for _, starter := range startes {

		typ := reflect.TypeOf(starter)

		//排除自己,避免调用boot.HookStarter.Stop而陷入逻辑死循环
		if typ.Elem().String() == "base.HookStarter" {
			continue
		}
		log.Info("stopping starter:", typ.Elem())
		starter.Stop(ctx)
	}
	os.Exit(0)
}
